import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import User from './components/Users.vue'
import Role from './components/Roles.vue'
import Cate from './components/Cates.vue'
// import Param from './components/params.vue'
import Param from './components/Params1.vue'
import Right from './components/Rights.vue'
import Report from './components/Reports.vue'
import Order from './components/Orders.vue'
// import Add from './components/Add.vue'
import Add from './components/add2.vue'
// import Order from './components/order.vue'
// import Good from './components/Goods.vue'
import Good from './components/Goods2.vue'
import Login from './views/Login.vue'
import AddGoods from './components/AddGoods'
import UpdateGoods from './components/UpdateGoods'

Vue.use(Router)

const router = new Router({
    routes: [{
        path: '/',
        redirect: '/login',
    }, {
        path: '/login',
        name: '/login',
        component: Login
    }, {
        path: '/home',
        name: 'home',
        component: Home,
        children: [{
            path: '/users',
            name: 'user',
            component: User,
        }, {
            path: '/roles',
            name: 'role',
            component: Role,
        }, {
            path: '/rights',
            name: 'right',
            component: Right,
        }, {
            path: '/goods',
            name: 'good',
            component: Good,

        }, {
            path: '/categories',
            name: 'cate',
            component: Cate,
        }, {
            path: '/params',
            name: 'param',
            component: Param,
        }, {
            path: '/reports',
            name: 'report',
            component: Report,
        }, {
            path: '/orders',
            name: 'order',
            component: Order,
        }, {
            path: '/goods/add',
            name: 'add',
            component: Add,
        }, {
            path: '/addgoods',
            name: 'addgoods',
            component: AddGoods
        }, {
            path: '/updategoods/:id',
            name: 'updategoods',
            component: UpdateGoods
        }]
    }]
})

router.beforeEach((to, from, next) => {
    /* must call `next` */
    // console.log(to);
    // console.log(from);
    // next();
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token');
    if (!tokenStr) {
        return next('/login');
    }
    next();
})

export default router;